package cn.neusoft.yp.service.impl;

import cn.neusoft.yp.mapper.OrderMapper;
import cn.neusoft.yp.mapper.ProdMapper;
import cn.neusoft.yp.pojo.Order;
import cn.neusoft.yp.pojo.OrderInfo;
import cn.neusoft.yp.pojo.OrderItem;
import cn.neusoft.yp.pojo.Product;
import cn.neusoft.yp.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gaosheng on 2017/4/27.
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper ;
    @Autowired
    private ProdMapper prodMapper ;

    public void addOrder(Order order, List<OrderItem> list) {
        //向orders表中添加一个订单
        orderMapper.addOrder(order);
        //遍历添加订单项
        for(OrderItem oi : list){
            //判断库存是否充足
            Product prod = prodMapper.findOne(oi.getProduct_id());
            if(prod.getPnum()<oi.getBuynum()){
                throw new RuntimeException("库存不足,id:"+prod.getId()+",name:"
                        +prod.getName()+",pnum:"+prod.getPnum());
            }
            //添加订单条目
            orderMapper.addOrderItem(oi);
            //修改库存
           prodMapper.updatePnum(prod.getId(), prod.getPnum()-oi.getBuynum());
        }
    }

    public List<OrderInfo> findAllOrderByUid(String uid) {
        List<OrderInfo> resultList = new ArrayList<OrderInfo>();
        //根据用户id查询该用户所有的orders表中的订单信息
        List<Order> orderList = orderMapper.findOrdersByUid(uid);
        //判断当前用户是否有订单
        if(orderList!=null){//该用户有订单
            List<OrderItem> itemList = orderMapper.findOrderItemsByUid(uid);
            //遍历orderList
            for(Order order:orderList){
                OrderInfo oi = new OrderInfo();
                oi.setOrder(order);
                //遍历itemList
                Map<Product,Integer> map = new HashMap<Product, Integer>();
                for(OrderItem item:itemList){
                    //判断当前订单条目item,是否是order下的订单条目
                    if(order.getId().equals(item.getOrder_id())){
                        Product prod = prodMapper.findOne(item.getProduct_id());
                        map.put(prod, item.getBuynum());
                    }
                }
                //将map对象赋值给oi的map属性
                oi.setMap(map);
                resultList.add(oi);
            }
        }
        return resultList;
    }
    //id为prodID 商品ID
    public void deleteOrder(String id) {
        //业务规则检验：只有未支付的订单，才可以被删除
        Order order = orderMapper.findOrder(id);
        //判断订单的支付状态
        if(order.getPaystate()==1){
            throw new RuntimeException("已支付的订单不能删除！");
        }
        //还原库存
        //根据订单id查询所有的订单项的集合
        List<OrderItem> itemList = orderMapper.findOrderItems(id);
        //遍历itemList集合
        if(itemList!=null){
            for(OrderItem item : itemList){
                Product prod = prodMapper.findOne(item.getProduct_id());
                //修改库存
                prodMapper.updatePnum(item.getProduct_id(),prod.getPnum()+item.getBuynum());
            }
            //删除订单项
            orderMapper.deleteOrderItems(id);
        }
        //删除订单
        orderMapper.deleteOrder(id);
    }

    public Order findOrderById(String orderId) {
        return orderMapper.findOrder(orderId);
    }

    public void updatePaystate(String r6_order, int i) {
        orderMapper.updatePaysstate(r6_order,i);
    }
}

